<?php 
namespace Home\Controller;


class BlogDetailsController extends BaseController{
    
    public function index(){
        
        $blog_id = I('get.blog_id');
        
        // 设置博客点击量
        $this->setBlogClick();
        
        // 获取博客内容
        $blog = M('blog')->find($blog_id);
        
        // 获取关键字
        $key_ids = M('article_key')->where(array('art_id'=>$blog['id']))->field('key_id')->select();
        
        $keys = array();
        $art_ids = array();
        foreach ($key_ids as $v){
            $keys[] = $v['key_id'];
            M('keys')->where(array('id'=>$v['key_id']))->setInc('count');
            $ids = M('article_key')->where(array('key_id'=>$v['key_id']))->field('art_id')->select();
            $art_ids = array_merge($art_ids,$ids);
        }
        if (!empty($keys)){
            $keys = M('keys')->where(array('id'=>array('in',$keys)))->select();
            $this->assign('keys',$keys);
        }
        
        // 获取相关推荐
        $all_id = array();
        foreach ($art_ids as $v){
            if (!in_array($v['art_id'], $all_id) && $v['art_id'] != $blog_id){
                $all_id[] = $v['art_id'];
            }
        }
        if (!empty($all_id)){
            $xiangguan = M('blog')->where(array('id'=>array('in',$all_id)))->alias('b')->join('blog_cat c on c.cat_id=b.cat_id')->field('b.id,b.title,c.cat_name')->order('count desc,id desc')->limit(5)->select();
            $this->assign('xiangguan',$xiangguan);
        }
        
        // 获取评论列表
        $comments = M('comment')->where(array('blog_id'=>$blog_id,'is_last'=>1))->order('time desc,id desc')->select();
        
        // 获取文章收藏状态
        $state = 0;   // 未收藏
        
        if(session('?user')){ // 已登录
            $info = M('collection')->where(array('art_id'=>$blog['id'],'user_id'=>session('user.id')))->find();
            if (!empty($info)){
                $state = $info['state'];
            }
        }
        
        // 传递数据
        $this->assign('state',$state);
        $this->assign('blog',$blog);
        $this->assign('blog_id',$blog_id);
        $this->assign('comments',$comments);
        
        $this->display('Blog/blog_details');
    }
    
    
    // 设置博客点击量
    protected function setBlogClick(){
        $blog_id = I('get.blog_id');
        // 设置博客点击数
        // 获取ip地址
        $ip = get_client_ip();
        
        $article_click = M('article_click')->where(array(array('blog_id'=>$blog_id),array('ip'=>$ip)))->find();
        
        if (empty($article_click)){
            $data = array(
                'ip'=>$ip,
                'blog_id'=>$blog_id,
                'click_count'=>1,
                'time'=>date('Y-m-d',time()),
            );
            M('article_click')->add($data);
            M('blog')->where(array('id'=>$blog_id))->setInc('count');
        }else {
            if ($article_click['time'] == date('Y-m-d',time())){ // 今天
                if ($article_click['click_count'] < 5){
                    M('article_click')->where(array(array('blog_id'=>$blog_id),array('ip'=>$ip)))->setInc('click_count');
                    M('blog')->where(array('id'=>$blog_id))->setInc('count');
                }
            }else { // 不是今天
                $data = array(
                    'click_count'=>1,
                    'time'=>date('Y-m-d',time()),
                );
                M('article_click')->where(array(array('blog_id'=>$blog_id),array('ip'=>$ip)))->save($data);
                M('blog')->where(array('id'=>$blog_id))->setInc('count');
            }
            
        }
    }

    // 收藏文章
    public function collection(){
        
        // 记录当前收藏状态
        $state = 0;
        
        // 获取博客id
        $blog_id = I('post.blog_id');
        
        // 获取用户id
        if (session('?user')){
            $user_id = session('user.id');
        }else {
            $this->ajaxReturn(array('status'=>0,'msg'=>'请先登录'));
        }
        $collection_M = M('collection');
        // 查询数据库中是否有收藏
        $info = $collection_M->where(array('art_id'=>$blog_id,'user_id'=>$user_id))->find();
        if (empty($info)){ // 第一次收藏
            
            $res = $collection_M->add(array(
                'art_id' => $blog_id,
                'user_id' => $user_id,
                'state' => 1,
                'time' => time()
            ));
            $state = 1;
        }else { 
            // 判断当前是否收藏
            if ($info['state'] == 1){ // 已收藏
                $res = $collection_M->save(array(
                    'id' => $info['id'],
                    'state' => 0,
                    'time' => time()
                ));
                $state = 0;
            }else { // 未收藏
                $res = $collection_M->save(array(
                    'id' => $info['id'],
                    'state' => 1,
                    'time' => time()
                ));
                $state = 1;
            }
        }
        if($res !== false){
            $this->ajaxReturn(array('status'=>1,'state'=>$state));
        }else {
            $this->ajaxReturn(array('status'=>0,'msg'=>操作失败));
        }
    }
}

?>